<div>
    <form action="./transferSnsDiary.php" method="POST">
        oldUid:<input type="text" name="oldUid" />
        newUid:<input type="text" name="newUid" />
        <input type="hidden" name="command" value="0" />
        <input type="submit" value="查询" />
    </form>
</div>
<br /><hr /><br />
<?php
set_time_limit(0);
isset($_REQUEST['oldUid'])  &&  trim($_REQUEST['oldUid']);
isset($_REQUEST['newUid'])  &&  trim($_REQUEST['newUid']);

define('WEB_SERVER_ROOT_PATH',  '/opt/php/sns/');
require(WEB_SERVER_ROOT_PATH. 'config/config.php');
$hostUid    =   DOP::request('hostUid');
define('HOST_UID', $hostUid);

$host       =   'XXT_DB_HOST';
$user       =   'xxtDev';
$passwd     =   'xxtDEV';
$dbConnect  =   mysql_connect($host, $user, $passwd);

$diary      =   new DiaryController();
$user       =   new UserController();
$statistics =   new UserStatisticsController();

if (!empty($_REQUEST['oldUid']))/*{{{*/
{
    echo '<fieldset><legend style="color:red">Old userInfo ', $_REQUEST['oldUid'], '</legend>';
    $oldInfo    =   getOldUserInfo($_REQUEST['oldUid']);
    if (empty($oldInfo))
    {
        echo 'Don\'t Search The old userInfo ';
    }

    echo ' id -> ', $oldInfo['id'], ' uid -> ', $oldInfo['uid'], ' account -> ', $oldInfo['account'], ' email -> ', $oldInfo['email'], ' <br /><hr />';

    $oldBlog    =   getOldBlogNum($_REQUEST['oldUid']);
    if (empty($oldBlog))
    {
        echo ' Don\'t Search blog! ';
    }
    else
    {
        echo ' oldBlog Num = ', $oldBlog['num'];
    }

    $oldTwitter =   getOldTwitterNum($_REQUEST['oldUid']);
    if (empty($oldTwitter))
    {
        echo ' Don\'t Search twitter! ';
    }
    else
    {
        echo ' oldTwitter Num = ', $oldTwitter['num'];
    }

    $oldPosition=   getOldPositionNum($_REQUEST['oldUid']);
    if (empty($oldPosition))
    {
        echo ' Don\'t Search position! ';
    }
    else
    {
        echo ' oldPosition Num = ', $oldPosition['num'];
    }
    echo '</fieldset><br />';
}/*}}}*/

if (!empty($_REQUEST['newUid']))/*{{{*/
{
    echo '<br /><fieldset><legend style="color:red">New userInfo ', $_REQUEST['newUid'], '</legend>';
    $newInfo    =   getNewUserInfo($_REQUEST['newUid']);
    if (empty($newInfo))
    {
        echo ' Don\'t Search The new userInfo ';
    }
    echo ' uid -> ', $newInfo['userInfo']['uid'], ' account -> ', $newInfo['userInfo']['account'], ' nickname -> ', $newInfo['userInfo']['nickname'], ' email -> ', $newInfo['userInfo']['email'], ' diaryNums -> ', $newInfo['userStatistics']['diaryTimes'], ' <br /><hr />';

    $diaryList  =   getNewUserDiaryList($_REQUEST['newUid']);

    if (empty($diaryList)   ||  !is_array($diaryList))
    {
        # var_dump($diaryList);
        echo ' Don\'t Search diary list! ';
    }
    else
    {
        foreach ($diaryList as  $diaryInfo)
        {
            echo date('Y-m-d H:i:s', $diaryInfo['time']), ' bodyId ->', $diaryInfo['bodyId'], ' title ->', $diaryInfo['title'], ' abbreviation->', $diaryInfo['abbreviation'], ' <br />';
        }
    }
    echo '</fieldset><br />';
}/*}}}*/

if (!empty($_REQUEST['oldUid']) &&  !empty($_REQUEST['newUid']) &&  empty($_REQUEST['command']))
{
?>
<br /><hr /><br />
<div>
    <form action="./transferSnsDiary.php" method="POST">
        oldUid:<input type="text" name="oldUid" value="<?=$_REQUEST['oldUid'] ?>"/>
        newUid:<input type="text" name="newUid" value="<?=$_REQUEST['newUid'] ?>"/>
        <input type="hidden" name="command" value="1" />
        <input type="submit" value="确认要导入数据吗？请确认新旧账号正确，数据一旦导入，无法撤销！" style="color:red" />
    </form>
</div>
<?php
}

if (!empty($_REQUEST['oldUid']) &&  !empty($_REQUEST['newUid']) &&  !empty($_REQUEST['command']))
{
    importBlog($_REQUEST['oldUid'], $_REQUEST['newUid']);
    importTwitter($_REQUEST['oldUid'], $_REQUEST['newUid']);
    importPosition($_REQUEST['oldUid'], $_REQUEST['newUid']);

    echo '<br /><hr /><br /><fieldset><legend>import Finished userInfo ', $_REQUEST['newUid'], '</legend>';
    $newInfo    =   getNewUserInfo($_REQUEST['newUid']);
    if (empty($newInfo))
    {
        echo ' Don\'t Search The new userInfo ';
    }
    echo ' uid -> ', $newInfo['userInfo']['uid'], ' account -> ', $newInfo['userInfo']['account'], ' nickname -> ', $newInfo['userInfo']['nickname'], ' email -> ', $newInfo['userInfo']['email'], ' diaryNums -> ', $newInfo['userStatistics']['diaryTimes'], ' <br /><hr />';

    $diaryList  =   getNewUserDiaryList($_REQUEST['newUid']);

    if (empty($diaryList)   ||  !is_array($diaryList))
    {
        var_dump($diaryList);
        echo ' Don\'t Search diary list! ';
    }
    else
    {
        foreach ($diaryList as  $diaryInfo)
        {
            echo date('Y-m-d H:i:s', $diaryInfo['time']), ' bodyId ->', $diaryInfo['bodyId'], ' title ->', $diaryInfo['title'], ' abbreviation->', $diaryInfo['abbreviation'], ' <br />';
        }
    }
    echo '</fieldset>';
}


function getOldUserInfo($uid)/*{{{*/
{
    global  $dbConnect;
    $dbSQL  =   'SELECT * FROM appat.app_snsuser WHERE uid ="'. $uid. '"';
    $dbQuery=   mysql_query($dbSQL, $dbConnect);

    if (empty($dbQuery))    return  FALSE;

    $info   =   mysql_fetch_assoc($dbQuery);

    if (empty($info['id'])) return  FALSE;

    return  $info;
}/*}}}*/

function getOldBlogNum($uid)/*{{{*/
{
    global  $dbConnect;
    $dbSQL  =   'SELECT bloguid as oldUid, count(*) as num FROM appat.app_blog WHERE bloguid ="'. $uid. '"';
    $dbQuery=   mysql_query($dbSQL, $dbConnect);

    if (empty($dbQuery))    return  FALSE;

    $info   =   mysql_fetch_assoc($dbQuery);

    if (empty($info['oldUid'])) return  FALSE;

    return  $info;
}/*}}}*/

function getOldTwitterNum($uid)/*{{{*/
{
    global  $dbConnect;
    $dbSQL  =   'SELECT twauthoruid as oldUid , count(*) num FROM appat.app_twitter WHERE twauthoruid="'. $uid. '"';
    $dbQuery=   mysql_query($dbSQL, $dbConnect);

    if (empty($dbQuery))    return  FALSE;

    $info   =   mysql_fetch_assoc($dbQuery);

    if (empty($info['oldUid'])) return  FALSE;

    return  $info;
}/*}}}*/

function getOldPositionNum($uid)/*{{{*/
{
    global  $dbConnect;
    $dbSQL  =   'SELECT posuid as oldUid , count(*) num FROM appat.app_userposition WHERE posuid="'. $uid. '"';
    $dbQuery=   mysql_query($dbSQL, $dbConnect);

    if (empty($dbQuery))    return  FALSE;

    $info   =   mysql_fetch_assoc($dbQuery);

    if (empty($info['oldUid'])) return  FALSE;

    return  $info;
}/*}}}*/

function getNewUserInfo($uid)/*{{{*/
{
    global  $dbConnect, $user;

    $info   =   $user->getUserInfo(array('uid' => $uid));

    if (empty($info['userInfo']['account']))    return  FALSE;

    return  $info;
}/*}}}*/

function getNewUserDiaryList($uid)/*{{{*/
{
    global  $dbConnect, $diary;

    $userinfo   =   array('uid' => $uid, 'length' => 100);
    $info   =   $diary->getDiaryList($userinfo);

    if (empty($info[0]['uid']))     return  FALSE;

    return  $info;
}/*}}}*/

function importBlog($oldUid, $newUid)/*{{{*/
{
    if (empty($oldUid)  ||  empty($newUid)) return  FALSE;

    global  $dbConnect, $diary, $user, $statistics;
    $dbSQL      =   'SELECT * FROM `appat`.`app_blog` WHERE bloguid ="'. $oldUid. '"';
    $dbQuery    =   mysql_query($dbSQL, $dbConnect);
    echo date('H:i:s'), '    ', $dbSQL, "\r\n";
    if (empty($dbQuery))    continue;
    $importDiary=   array('success' => 0, 'fail' => 0);
    $errorDiary =   array();
    while ($tmp =   mysql_fetch_assoc($dbQuery))
    {
        $diaryInfo  =   array();
        $diaryInfo['uid']       =   $newUid;
        $diaryInfo['title']     =   $tmp['blogtitle'];
        $diaryInfo['content']   =   $tmp['blogcont'];
        $diaryInfo['saveTime']  =   strtotime($tmp['blogtime']);
        $diaryInfo['weather']   =   $tmp['blogweather'];
        $diaryInfo['emotion']   =   $tmp['blogemotion'];
        $diaryInfo['textSize']  =   $tmp['blogtextsize'];
        $diaryInfo['theme']     =   $tmp['blogtheme'];

        $diary->addDiary($diaryInfo);
        if (empty(AC::$errorNo))
        {
            $importDiary['success']++;
            $diaryInfo['diaryTimes']    =   1;
            $statistics->userStatistics($diaryInfo);
        }
        else
        {
            var_dump($diaryInfo);
            var_dump(mysql_error());
            $importDiary['fail']++;
            $errorDiary[]   =   AC::$errorNo;
            AC::$errorNo    =   NULL;
        }
        unset($diaryInfo);
    }
    print_r($importDiary);
    print_r($errorDiary);
    unset($importDiary);
    unset($errorDiary);

    return  TRUE;
}/*}}}*/

function importTwitter($oldUid, $newUid)/*{{{*/
{
    if (empty($oldUid)  ||  empty($newUid)) return  FALSE;

    global  $dbConnect, $diary, $user, $statistics;
    $dbSQL      =   'SELECT * FROM `appat`.`app_twitter` WHERE twauthoruid ="'. $oldUid. '"';
    $dbQuery    =   mysql_query($dbSQL, $dbConnect);
    echo date('H:i:s'), '    ', $dbSQL, "\r\n";
    if (empty($dbQuery))    continue;
    $importDiary=   array('success' => 0, 'fail' => 0);
    $errorDiary =   array();
    while ($tmp =   mysql_fetch_assoc($dbQuery))
    {
        $diaryInfo  =   array();
        $diaryInfo['uid']       =   $newUid;
        $diaryInfo['title']     =   $tmp['twcont'];
        $diaryInfo['content']   =   $tmp['twcont'];
        $diaryInfo['saveTime']  =   strtotime($tmp['twtime']);

        $diary->addDiary($diaryInfo);
        if (empty(AC::$errorNo))
        {
            $importDiary['success']++;
            $diaryInfo['diaryTimes']    =   1;
            $statistics->userStatistics($diaryInfo);
        }
        else
        {
            var_dump($diaryInfo);
            var_dump(mysql_error());
            $importDiary['fail']++;
            $errorDiary[]   =   AC::$errorNo;
            AC::$errorNo    =   NULL;
        }
        unset($diaryInfo);
    }
    print_r($importDiary);
    print_r($errorDiary);
    unset($importDiary);
    unset($errorDiary);

    return  TRUE;
}/*}}}*/

function importPosition($oldUid, $newUid)/*{{{*/
{
    if (empty($oldUid)  ||  empty($newUid)) return  FALSE;

    global  $dbConnect, $diary, $user, $statistics;
    $dbSQL      =   'SELECT * FROM `appat`.`app_userposition` WHERE posuid="'. $oldUid. '"';
    $dbQuery    =   mysql_query($dbSQL, $dbConnect);
    echo date('H:i:s'), '    ', $dbSQL, "\r\n";
    if (empty($dbQuery))    continue;
    $importDiary=   array('success' => 0, 'fail' => 0);
    $errorDiary =   array();
    while ($tmp =   mysql_fetch_assoc($dbQuery))
    {
        $diaryInfo  =   array();
        $diaryInfo['uid']       =   $newUid;
        $diaryInfo['title']     =   '城市脚印';
        $diaryInfo['content']   =   $tmp['posaction'];
        $diaryInfo['saveTime']  =   strtotime($tmp['postime']);

        $diary->addDiary($diaryInfo);
        if (empty(AC::$errorNo))
        {
            $importDiary['success']++;
            $diaryInfo['diaryTimes']    =   1;
            $statistics->userStatistics($diaryInfo);
        }
        else
        {
            var_dump($diaryInfo);
            var_dump(mysql_error());
            $importDiary['fail']++;
            $errorDiary[]   =   AC::$errorNo;
            AC::$errorNo    =   NULL;
        }
        unset($diaryInfo);
    }
    print_r($importDiary);
    print_r($errorDiary);
    unset($importDiary);
    unset($errorDiary);

    return  TRUE;
}/*}}}*/


